package _17_剑指Offer;

import java.util.ArrayList;
import java.util.List;

public class _05_剑指Offer替换空格 {

    public static void main(String[] args) {

        _05_剑指Offer替换空格 v = new _05_剑指Offer替换空格();

        String str = "We are happy";
        System.out.println(v.replaceSpace(str));

    }


    public String replaceSpace(String s) {

        List<Character> results = new ArrayList<>();
        int len = s.length();

        for (int i = 0; i < len; i++) {
            char curChar = s.charAt(i);
            if (curChar == ' ') {
                results.add('%');
                results.add('2');
                results.add('0');
            } else {
                results.add(curChar);
            }
        }
        int resultSize = results.size();
        char[] chars = new char[resultSize];
        for (int i = 0; i < resultSize; i++) {
            chars[i] = results.get(i);
        }
        return new String(chars);
    }

}
